package www.rlicker.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

import www.rlicker.model.GroupResource;
import www.rlicker.model.SysRole;
import www.rlicker.model.UserSourceDatail;
import www.rlicker.mybatis.Criteria;
import www.rlicker.mybatis.PageResult;
import www.rlicker.service.IGroupResourceService;
import www.rlicker.service.ISysRoleService;
import www.rlicker.service.IUserSourceDatailService;
import www.rlicker.utils.BaseController;
import www.rlicker.utils.JsonResult;
import www.rlicker.utils.StringUtil;

@Controller
@RequestMapping(value = "/usersource")
public class UserSourceDatailController extends BaseController {

	@Autowired
	private IUserSourceDatailService service;
	
	@Autowired
	private ISysRoleService sysRoleService;
	
	@Autowired
	private IGroupResourceService groupResourceService;
	

	@RequestMapping("/showlist")
	public String showlist(HttpServletRequest request,ModelMap map) {
		SysRole sysRole = sysRoleService.queryByRoleKey(getUserId(request));
		if (sysRole != null) {
			Criteria<GroupResource> params = new Criteria<GroupResource>();
			params.addParam("resourceIdList", getResourceId(sysRole.getResourceId()));
			params.addParam("parent_resource_id",new Long(request.getParameter("id")));
			params.addParam("status", 0);
			params.addParam("showType", 1);
			List<GroupResource> lists = groupResourceService.queryPageList(params);
            map.put("gpLists", JSONArray.toJSONString(lists));
		}
        map.put("id", request.getParameter("id"));
		return "userSource/userSourceList";
	}

	@RequestMapping("/querypage")
	@ResponseBody
	public PageResult queryPage(HttpServletRequest request) {
		Criteria<UserSourceDatail> param = new Criteria<UserSourceDatail>();
		param.addParam("income_status", RETURN_JSONFIVE);
		param.addParam("type", RETURN_STRING);
		if (!StringUtil.isNullOrBlank(request.getParameter("startTime"))) {
			param.addParam("startTime", request.getParameter("startTime"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("endTime"))) {
			param.addParam("endTime", request.getParameter("endTime"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("phone"))) {
			param.addParam("phone", request.getParameter("phone"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("startData"))) {
			param.addParam("startData", request.getParameter("startData"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("endData"))) {
			param.addParam("endData", request.getParameter("endData"));
		}
	    if (!StringUtil.isNullOrBlank(request.getParameter("pageNumber"))) {
	    	param.addParam("startIndex", Integer.parseInt(request.getParameter("pageNumber")));
	    }
	    if (!StringUtil.isNullOrBlank(request.getParameter("pageSize"))) {
	    	param.addParam("endPage", Integer.parseInt(request.getParameter("pageSize")));
	    }
	    param.addParam("data_content", "充值积分");
		List<Map<String, Object>> list = service.queryPage(param);
		PageResult pageResult = new PageResult();
		pageResult.setTotal(service.queryPageCount(param));
		pageResult.setRows(list);
		return pageResult;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public JsonResult delete(String id,String nickname){
		String ret = service.deleteById(Long.valueOf(id));
		JsonResult jr = JsonResult.newInstance(); 
		String content="ɾ"+nickname+"ͳ";
		insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
}
